package cn.i7baozh.cloud.consumer.controller;

import cn.i7baozh.cloud.api.dto.PayDTO;
import cn.i7baozh.cloud.api.dto.ReturnResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author zhangbaoqi532
 * @date 2024/12/25
 */
@Slf4j
@RestController
public class ConsumerController {

    //    private static final String provider_interface_url = "http://localhost:8001";
    private static final String provider_interface_url = "http://cloud-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/get/{id}")
    public ReturnResponse<PayDTO> get(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(provider_interface_url + "/provider/pay/get/" + id, ReturnResponse.class);
    }

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/consumer/getProviders")
    public ReturnResponse<String> getProviders() {
        List<String> allServices = discoveryClient.getServices();
        log.info("所有的服务名称：{}", allServices);
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-provider-payment");
        log.info("cloud-provider-payment 提供者相关信息");
        for (ServiceInstance instance : instances) {
            log.info("id:{},host:{},meta:{},port:{},uri:{},scheme:{}", instance.getInstanceId(),
                    instance.getHost(), instance.getMetadata(), instance.getPort(), instance.getUri()
                    , instance.getScheme());
        }
        return ReturnResponse.success("te");
    }

    @GetMapping(value = "/consumer/getServerInfo")
    public ReturnResponse<String> getServerInfo() {
        return restTemplate.getForObject(provider_interface_url + "/provider/pay/getServerInfo", ReturnResponse.class);
    }
}
